package org.example.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.example.template.domain.Sensor;
import org.example.template.mapper.SensorMapper;
import org.example.template.mapper.UsersMapper;
import org.example.template.services.ExportService;
import org.example.template.utils.AjaxResult;
import org.example.template.utils.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

@Slf4j//对日志依赖的引用
@RestController
public class ControllerDemo {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private SensorMapper sensorMapper;
    @Autowired
    public RedisService redisService;
    @Autowired
    private ExportService testService;
    @GetMapping("/addsensordata")
    public StringBuffer addSensorData(@PathParam("head") Integer head, @PathParam("pm1") Integer pm1, @PathParam("pm2") Integer pm2, @PathParam("pm3") Integer pm3, @PathParam("pm4") Integer pm4, @PathParam("pm5") Integer pm5, @PathParam("pm6") Integer pm6, @PathParam("pm7") Integer pm7, @PathParam("temperature") Integer temperature, @PathParam("humidity") Integer humidity, @PathParam("temperature1") Integer temperature1, @PathParam("humidity1") Integer humidity1,@PathParam("pressure") Integer pressure) {
        Sensor sensor = new Sensor();
        if (head == 66) {
            sensor.setHumidity(Float.valueOf(humidity) / 10);
            sensor.setHumidity1(Float.valueOf(humidity1) / 100);
            sensor.setTemperature(Float.valueOf(temperature) / 10);
            sensor.setTemperature1(Float.valueOf(temperature1) / 100);
            sensor.setAirPressure(Float.valueOf(pressure)/1000);
            sensor.setPm1(Float.valueOf(pm1));
            sensor.setPm2(Float.valueOf(pm2));
            sensor.setPm3(Float.valueOf(pm3));
            sensor.setPm4(pm4);
            sensor.setPm5(pm5);
            sensor.setPm6(pm6);
            sensor.setPm7(pm7);
            sensorMapper.insert(sensor);
            // 创建Gson对象
            Gson gson = new Gson();
            // 将对象转换为JSON字符串
            String json = gson.toJson(sensor);
            redisService.setCacheObject("redisStorageAirNewest:data", json); // 将数据存储到Redis中
        }
        StringBuffer sb = new StringBuffer();
        sb.append("@");
        sb.append(redisService.getCacheObject("redisStorageAir:data").toString());
        return sb.append("\r\n");
    }


    @GetMapping("/gethistoryair")
    public AjaxResult getHistoryair(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @PathParam("startTime") String startTime, @PathParam("endTime") String endTime
    ) {
        Page<Sensor> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Sensor> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("create_time", startTime); // "date_column"是数据库中存储时间的字段
        queryWrapper.le("create_time", endTime);
        return AjaxResult.success(sensorMapper.selectPage(page, queryWrapper));
    }

    /**
     * 使用Redis存储数据。
     *
     * @param key 需要存储数据的键名。
     * @return 返回操作结果，成功则返回AjaxResult的success方法结果。
     */
    @GetMapping("/redisstorage")
    public AjaxResult redisStorage(@RequestParam("value") String value) {
        // 将指定的key存储到Redis中，键名为"redisStorageCar:data"
        redisService.setCacheObject("redisStorageAir:data", value);
        return AjaxResult.success();
    }
    @GetMapping("/demo")
    public AjaxResult demo(@RequestParam("value") String value) {
        // 将指定的key存储到Redis中，键名为"redisStorageCar:data"
        log.info(value);
        return AjaxResult.success();
    }
    @GetMapping("/getairnewest")
    public AjaxResult getairnewest() {
        // 将指定的key存储到Redis中，键名为"redisStorageCar:data"
        return AjaxResult.success().put("data",redisService.getCacheObject("redisStorageAirNewest:data"));
    }
    @GetMapping("/getredisstorage")
    public AjaxResult getRedisStorage() {
        // 获取redisStorageCar:data的存储数据
        return AjaxResult.success(redisService.getCacheObject("redisStorageAir:data").toString());
    }

    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    @ApiOperation(value = "导出excel", produces = "application/octet-stream")
    public void exportCorpLoanDemand(@PathParam("startTime") String startTime, @PathParam("endTime") String endTime, HttpServletResponse response) {

        QueryWrapper<Sensor> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("create_time", startTime); // "date_column"是数据库中存储时间的字段
        queryWrapper.le("create_time", endTime);
        List<Sensor> list = sensorMapper.selectList(queryWrapper);
        List<List<String>> templist = new ArrayList<>();

        for (Sensor sensor : list) {
            List<String> templist2 = new ArrayList<>();
            templist2.add(sensor.getPm1().toString());
            templist2.add(sensor.getPm2().toString());
            templist2.add(sensor.getPm3().toString());
            templist2.add(sensor.getPm4().toString());
            templist2.add(sensor.getPm5().toString());
            templist2.add(sensor.getPm6().toString());
            templist2.add(sensor.getPm7().toString());
            templist2.add(sensor.getTemperature().toString());
            templist2.add(sensor.getHumidity().toString());
            templist2.add(sensor.getAirPressure().toString());
            templist2.add(sensor.getCreateTime());
            templist.add(templist2);
        }
        testService.exportExcel(templist, response);
    }
}
/**
 * 硬件发送数据到服务器，服务器接受到数据给mysql和redis各存储一份。返回数据取redis里面的数据(来源:前端调用redisstorage存储的数据)
 */
